package com.ternnetwork.configuration.service.impl.persistence;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.ternnetwork.commons.model.http.BaseResponse;
import com.ternnetwork.configuration.dao.persistence.IdGeneratorDao;
import com.ternnetwork.configuration.entity.persistence.IdGenerator;
import com.ternnetwork.configuration.service.persistence.IdGeneratorService;
import com.ternnetwork.persistence.page.BootstrapGrid;
import com.ternnetwork.persistence.page.Page;





@Service("idGeneratorService")
public class IdGeneratorServiceImpl implements IdGeneratorService {

	@SuppressWarnings("restriction")
	@Resource
	private IdGeneratorDao idGeneratorDao;
	@Override
	public BaseResponse  idoAdd(IdGenerator t) {
		
		BaseResponse res=new BaseResponse();
		
		if(StringUtils.isEmpty(t.getGenName())){
			res.setStatus(0);
			res.setMessage("操作失败，名称不能为空！");
			return res;
		}
		if(t.getGenValue()==0){
			res.setStatus(0);
			res.setMessage("操作失败，下一个值不能为0！");
			return res;
		}
		List<Object> list=idGeneratorDao.queryAllObject("select t.id from IdGenerator t where t.genName=?1",t.getGenName());
		if(list.size()>0){
			res.setStatus(0);
			res.setMessage("操作失败，名称已经存在！");
			return res;
		}
		t.setGenName(t.getGenName().toUpperCase());
		idGeneratorDao.persist(t);
		res.setStatus(1);
		res.setMessage("操作成功！");
		return res;
	}
	@Override
	public BaseResponse idoUpdate(IdGenerator t) {
		
		BaseResponse res=new BaseResponse();
		
		if(StringUtils.isEmpty(t.getGenName())){
			res.setStatus(0);
			res.setMessage("操作失败，名称不能为空！");
			return res;
		}
		if(t.getGenValue()==0){
			res.setStatus(0);
			res.setMessage("操作失败，下一个值不能为0！");
			return res;
		}
		List<Object> list=idGeneratorDao.queryAllObject("select t.id from IdGenerator t where t.genName=?1 and t.id!=?2",t.getGenName(),t.getId());
		if(list.size()>0){
			res.setStatus(0);
			res.setMessage("操作失败，名称已经存在！");
			return res;
		}
		t.setGenName(t.getGenName().toUpperCase());
		idGeneratorDao.saveOrUpdate(t);
		res.setStatus(1);
		res.setMessage("操作成功！");
		return res;
	}
	@Override
	public BaseResponse idoDelete(long id) {
		BaseResponse res=new BaseResponse();
		IdGenerator t=idGeneratorDao.findById(IdGenerator.class, id);
		if(t==null){
			res.setStatus(0);
			res.setMessage("操作失败，对象不存在！");
			return res;
		}
		idGeneratorDao.delete(t);
		res.setStatus(1);
		res.setMessage("操作成功！");
		return res;
	}
	
	@Override
	public String queryToJsonStr(IdGenerator t,Page page) {
		Page<IdGenerator> result=query(t, page);
		BootstrapGrid grid=new BootstrapGrid(result);
		return grid.toString();
	}
	
	public Page<IdGenerator> query(IdGenerator t,Page page) {
		StringBuffer jpql=new StringBuffer("from IdGenerator t where 1=1");
		List<Object> params=new ArrayList<Object>();
		if(StringUtils.isNotEmpty(t.getGenName())){
			params.add(t.getGenName().toUpperCase()+"%");
			jpql.append(" and t.genName like?"+params.size());
		}
		return idGeneratorDao.query("select count(t.id) "+jpql.toString(),jpql.toString(), page, params.toArray());
	}
	

}
